<template>
  <!-- 添加或修改商品规格属性对话框 -->
  <el-dialog :close-on-click-modal="false" :title="formType" :visible.sync="open" @close="cancel"
             width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-row v-for="item in skuList" v-if="formType == $formType.add">
        <el-col :span="12">
          <el-form-item :label="item.name" prop="specName">
            <el-input v-model="item.specName" :placeholder="'请输入'+item.name"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="formType == $formType.edit">
        <el-col :span="12">
          <el-form-item label="SKU" prop="image">
            {{form.specsName}}
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="商品图片" prop="image">
            <el-image class="res-image" v-if="form.image != null && form.image != ''" :src="form.image"
                      :preview-src-list="[form.image]"/>
            <upload-oss-btn ref="upload" :btnText="form.image == null || form.image == ''? '快速上传':'重新上传'"
                            remark="宽:350  高:350"
                            @onUploadResult="onUploadResult"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="原价" prop="originalPrice">
            <el-input-number v-model="form.originalPrice" :min="0.01" :precision="2"
                             :max="99999999"
                             placeholder="请输入原价"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="现价" prop="presentPrice">
            <el-input-number v-model="form.presentPrice" :min="0.01" :precision="2"
                             :max="99999999" placeholder="请输入现价"/>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row v-if="formType != $formType.edit">
        <el-col :span="12">
          <el-form-item label="初始库存" prop="inventoryTotal">
            <el-input-number v-model="form.inventoryTotal" :min="1" :precision="0" :max="99999"
                             placeholder="请输入当前库存总数"/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="排序(倒序)">
            <el-input-number v-model="form.sort" :min="1" placeholder="请输入排序"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictValue">{{dict.dictLabel}}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import operationModule from '@/mixins/operation-module'
  import {
    addGoods_spec,
    updateGoods_spec
  } from '@/api/goods/goods_spec'

  import UploadOssBtn from '@/components/UploadOssBtn'
  import {
    skuList
  } from '@/api/goods/goods'

  export default {
    name: 'add-or-update',
    mixins: [operationModule],
    components: { UploadOssBtn },
    data() {
      return {
        // 设置属性
        mixinOperationModuleOptions: {
          saveURL: addGoods_spec, // 提交数据接口，api地址
          updateURL: updateGoods_spec // 提交数据接口，api地址
        },
        moduleName: 'Goods_spec',
        open: false,
        // 弹出层标题
        //表单操作类型
        formType: this.$formType.add,
        goodsId: null,
        // 表单参数
        form: {},
        skuList: [],
        // 状态;0=已上架,1=未上架字典
        statusOptions: [],
        // 表单校验
        rules: {}
      }
    },
    methods: {
      onUploadResult(image) {
        this.form.image = image
      },
      init(type) {
        this.reset()
        this.open = true
        if (type == this.$formType.add) {
        } else if (type == this.$formType.edit) {
        } else if (type == this.$formType.detail) {
        }
        this.formType = type
      },
      initHttp() {
        skuList({ goodsId: this.goodsId }).then(response => {
          this.skuList = response.data
        })
      },
      onStartSbmitForm() {
        const goodsId = this.$route.params && this.$route.params.goodsId
        this.form.goodsId = goodsId
        this.form.skuList = this.skuList
        return true
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          goodsId: undefined,
          number: undefined,
          originalPrice: undefined,
          presentPrice: undefined,
          inventoryTotal: undefined,
          soldoutTotal: undefined,
          status: '0',
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          image: undefined,
          skuId: undefined,
          skuList: null,
          remark: undefined
        }
        this.goodsId = null
        this.resetForm('form')
      },
      cancel() {
        this.$emit('on-close')
        this.open = false
        this.skuList = []
      }
    },
    created() {
      this.reset()
    }
  }
</script>
<style lang="scss">
  .res-image {
    width: 120px;
    height: 120px;
    border-radius: 6px;
  }

</style>
